package com.cc.eds.model;

import com.thoughtworks.xstream.annotations.XStreamAlias;
import com.thoughtworks.xstream.annotations.XStreamOmitField;
import lombok.Getter;
import lombok.Setter;
import org.dromara.hutool.core.text.StrUtil;

import java.io.Serial;
import java.io.Serializable;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.List;

/**
 * EdsData
 *
 * @author aa
 * @version 1.0
 * @since 2025/2/8
 */
@Getter
@Setter
@XStreamAlias("MESSAGE")
public class EdsDataXml implements Serializable {

    @Serial
    private static final long serialVersionUID = 6134162716824187164L;

    @XStreamAlias("DATASET")
    private DataSet dataset;

    /**
     * 映射关系list
     */
    @XStreamOmitField
    private List<DataInfo> dataInfoList;

    /**
     * bcp文件夹路径
     */
    private Path bcpDirPath;

    /**
     * 获取dataInfoList(bcp文件->表的映射关系)
     */
    public List<DataInfo> getDataInfoListBySort() {
        if (dataInfoList == null) {
            dataInfoList = dataset.getDataInfoList().get(0).getDatasetByDn(Eds.FORMAT_BCP_DN).getDataInfoList();
            //按表名排序,确保wm0101基本信息优先入库
            dataInfoList.sort(Comparator.comparing(dataInfo -> dataInfo.getValByIk(Eds.TABLE_NAME_IK)));
        }
        return dataInfoList;
    }

    /**
     * 根据class获取映射关系
     */
    public DataInfo getDataInfo(String tableName) {

        for (DataInfo dataInfo : getDataInfoListBySort()) {
            String bcpTableName = dataInfo.getValByIk(Eds.TABLE_NAME_IK);
            if (StrUtil.equalsIgnoreCase(tableName, bcpTableName)) {
                return dataInfo;
            }
        }
        return null;
    }

    public List<Path> getBcpList(DataInfo dataInfo) {
        return dataInfo.getBcpFileList(bcpDirPath);
    }
}
